﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DFYE.Web.Models;
using JULONG.WEB.Library;
using JULONG.WEB.Library.expViewResult;
namespace DFYE.Web.Areas.manage.Controllers
{
    public class guestBookController : Controller
    {
        DataSession<entityDBContainer, guestBook> db = new DataSession<entityDBContainer, guestBook>();

        //
        // GET: /manage/guestBook/
        public ActionResult Index(int id)
        {
            ViewBag.student = db.newSession<student>().get(d => d.ID == id);

            ViewData.Model = db.query().Where(d => d.studentID == id);
            return View();
        }
        public ActionResult topIndex()
        {
            ViewData.Model = db.query().OrderByDescending(d => d.able).ThenByDescending(d => d.datetime);
            return View();
        }
        //
        // GET: /manage/guestBook/Edit/5

        public ActionResult Edit(int id)
        {
            var gb = db.get(d => d.ID == id);
            ViewData.Model = gb;
            ViewBag.student = db.newSession<student>().get(d => d.ID == gb.studentID);
            return View();
        }

        //
        // POST: /manage/guestBook/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = db.get(d => d.ID == id);

            ViewBag.student = db.newSession<student>().get(d => d.ID == entity.studentID);
            try
            {
                UpdateModel(entity);

                db.save();
                return RedirectToAction("Index");
            }
            catch
            {
                ViewData.Model = entity;
                return View();
            }
        }
        [HttpGet]
        public ActionResult reply(int id)
        {

            var entity = db.get(d => d.ID == id);
            ViewBag.student = db.newSession<student>().get(d => d.ID == entity.studentID);

            ViewData.Model = entity;
            return View();

        }
        [HttpPost]
        public ActionResult reply(int id, string content)
        {

            var entity = db.get(d => d.ID == id);
            ViewBag.student = db.newSession<student>().get(d => d.ID == entity.studentID);
            if(!string.IsNullOrWhiteSpace(content)){
                string _content = content.Replace("[div", "<div").Replace("[/div]", "</div>").Replace("\"]", "\">").Replace("{datetime}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                ViewBag.student = db.newSession<student>().get(d => d.ID == entity.studentID);
                entity.content += _content;

                entity.replyDatetime = DateTime.Now;
                entity.able = false;//如果为用户回复able:true，管理员回复able:false;
                db.save();
            }
            ViewData.Model = entity;
            return View();

        }
        //
        // GET: /manage/guestBook/Delete/5

        public ActionResult Delete(int id,int studentID=0 )
        {
            try
            {
                // TODO: Add delete logic here
                db.delete(d => d.ID == id);

                if (studentID == 0) {
                    return RedirectToAction("topIndex");
                }
                return RedirectToAction("Index", new { id = studentID });
            }
            catch
            {
                return RedirectToAction("topIndex");
            }
        }


    }
}
